<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-07-04 16:23:48
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\System;

use App\Http\Services\Service;
use App\Models\TableModel;
use App\Repositories\TableColumnRepository;
use App\Repositories\TableRepository;
use App\Repositories\VersionRepository;
use Illuminate\Http\Request;

/**
 * @property TableRepository $repository
 * @property TableColumnRepository $column
 */
class TableService extends Service
    {

    public function __construct(TableRepository $repository, TableColumnRepository $column)
        {
        $this->repository = $repository;
        $this->column     = $column;
        }
    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    public function info()
        {

        $request = request();
        if ($request->has("id"))
            $this->repository->whereId($request->id);
        else if ($request->has("code"))
            $this->repository->whereCode($request->code);
        else {
            fail('参数不正确');
            }
        return $this->repository->getConfig($request->has("code"));
        }


    protected function save(Request $request)
        {
        $data = $request->toArray();
        //修改，更新系统版别，后台更新缓存
        if (($data['id'] ?? 0) > 0)
            VersionRepository::newver();
        unset($data['excelurl']);
        return $this->repository->autosave($data, true);
        }
    public function getCategory()
        {
        return $this->repository->getCategory();
        }
    protected function deleteCategory($request)
        {
        return $this->repository->deleteCategory($request->input("id"));
        }

    protected function delete()
        {
        $this->repository->with(["columns", "allbuttons", 'forms', 'reports'])->whereIn("id", $this->getRequestIds())->autodelete();

        return "delete success!";
        }
    public function authColumn()
        {
        return $this->column->with(['gridtable'])->whereAuth(1)->getList();
        }
    }